package com.hospital.service.impl;

import com.hospital.config.SystemConfig;
import com.hospital.dao.AdminDao;
import com.hospital.dao.MenuDao;
import com.hospital.dto.TreeMenu;
import com.hospital.entity.Admin;
import com.hospital.entity.Menu;
import com.hospital.entity.User;
import com.hospital.service.intf.AdminService;
import com.hospital.util.PassUtils;
import com.hospital.util.StringUtil;
import com.hospital.vo.R;
import com.hospital.vo.UserTreeMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao dao;

    @Autowired
    private MenuDao menuDao;


    @Override
    public R selectOne(String verifyCode,Admin admin , HttpSession session) {
        String code = (String) session.getAttribute("code");
        session.removeAttribute("code");
        if (verifyCode.equalsIgnoreCase(code)){
        //1.校验参数
        if(StringUtil.isNotEmpty(admin.getName(), admin.getPassword())){
            //2.查询数据库
            Admin admin1=dao.selectOne(admin);
            //3.验证是否存在用户
            if(admin1!=null){
                    //5.校验密码
                    if(admin1.getPassword().equals(PassUtils.aesenc(SystemConfig.PASS_KEY,admin.getPassword()))){
                        //6.登录成功 存储到Session
                        session.setAttribute("admin",admin1);
                        //7.返回
                        return R.ok(admin1);
                }
            }
        }

        }else {
            return R.fail("验证码错误");
        }
        //防止撞库
        return R.fail("亲，账号或密码错误");
    }

    @Override
    public HashMap initmenu(HttpSession session) {
        //1.获取登录用户

        Admin admin= (Admin) session.getAttribute("admin");
        User user= (User) session.getAttribute("user");
        if(admin != null){
            //2.查询当前用户的菜单
            System.out.println(admin.getId());
            List<Menu> menus=menuDao.selectByUid(admin.getId());
            //3.按照前端要求 init.json 组装数据
            List<TreeMenu> treeMenus=new ArrayList<>();
            menus.forEach(m->{
                //4.生成菜单对象
                TreeMenu menu=new TreeMenu(m.getId(),m.getName(),m.getIcon(),m.getUrl(),"_self",new ArrayList<>());
                //5.如果是一级菜单，直接添加
                if(m.getLevel()==1){
                    treeMenus.add(menu);
                }else {
                    //6.二级菜单 ，需要找到一级菜单，然后再添加
                    for(TreeMenu tm :treeMenus){
                        if(tm.getId().equals(m.getPid())){
                            //把二级添加到一级下
                            tm.getChild().add(menu);
                            break;
                        }
                    }
                }
            });
            //7.创建 menuInfo的数据
            UserTreeMenu utm=new UserTreeMenu();
            utm.setChild(treeMenus);
            List list=new ArrayList();
            list.add(utm);

            //8.创建homeInfo的数据
            HashMap map4=new HashMap();
            map4.put("title","主页");
            map4.put("href","page/welcome-1.html?t=1");

            //9.创建logoInfo的数据
            HashMap map5=new HashMap();
            map5.put("image","images/logo.png");

            //10.组装最后要的数据
            LinkedHashMap map6=new LinkedHashMap();
            map6.put("homeInfo",map4);
            map6.put("logoInfo",map5);
            map6.put("menuInfo",list);
            return map6;
        }else if(user != null){
            //2.查询当前用户的菜单
            List<Menu> menus=menuDao.selectByUid2(user.getId());
            //3.按照前端要求 init.json 组装数据
            List<TreeMenu> treeMenus=new ArrayList<>();
            menus.forEach(m->{
                //4.生成菜单对象
                TreeMenu menu=new TreeMenu(m.getId(),m.getName(),m.getIcon(),m.getUrl(),"_self",new ArrayList<>());
                //5.如果是一级菜单，直接添加
                if(m.getLevel()==1){
                    treeMenus.add(menu);
                }else {
                    //6.二级菜单 ，需要找到一级菜单，然后再添加
                    for(TreeMenu tm :treeMenus){
                        if(tm.getId().equals(m.getPid())){
                            //把二级添加到一级下
                            tm.getChild().add(menu);
                            break;
                        }
                    }
                }
            });
            //7.创建 menuInfo的数据
            UserTreeMenu utm=new UserTreeMenu();
            utm.setChild(treeMenus);
            List list=new ArrayList();
            list.add(utm);

            //8.创建homeInfo的数据
            HashMap map4=new HashMap();
            map4.put("title","主页");
            map4.put("href","page/welcome-1.html?t=1");

            //9.创建logoInfo的数据
            HashMap map5=new HashMap();
            map5.put("image","images/logo.png");

            //10.组装最后要的数据
            LinkedHashMap map6=new LinkedHashMap();
            map6.put("homeInfo",map4);
            map6.put("logoInfo",map5);
            map6.put("menuInfo",list);
            return map6;
        }else{
            HashMap map=new HashMap();
            map.put("code",400);
            map.put("msg","亲，请登录！");
            return map;
        }
    }
}
